package com.promis.widget.imgzoom;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ImageView;

import com.promis.util.CollectionUtils;
import com.promis.widget.R;

import java.util.List;

/**
 * Created by jidanbo on 2018/6/6.
 */

public class ThumbnailAdapter extends RecyclerView.Adapter<ThumbnailAdapter.ViewHolder> {

    private Context context;
    private List<String> imageUrlList;
    private SparseArray imageViewMap = new SparseArray();

    public ThumbnailAdapter(Context context) {
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ThumbnailView imageView = new ThumbnailView(context);
        imageView.setSquare(true);

        AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        imageView.setLayoutParams(layoutParams);
//        imageView.setPadding(0, 0, 9, 9);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        return new ViewHolder(context, imageView);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {

        if (CollectionUtils.isNullOrEmpty(imageUrlList)) return;

        if ((imageUrlList.size() == 4 && position < 2) || imageUrlList.size() != 4) {
            String imageUrl = imageUrlList.get(position);
            ((ThumbnailView) holder.itemView).setImageUrl(imageUrl);
            imageViewMap.put(position, holder.itemView);
        } else if (imageUrlList.size() == 4 && position > 2) {
            String imageUrl = imageUrlList.get(position - 1);
            ((ThumbnailView) holder.itemView).setImageUrl(imageUrl);
            imageViewMap.put(position - 1, holder.itemView);
        } else {
            ((ThumbnailView) holder.itemView).setImageUrl(null);
        }
    }

    @Override
    public int getItemCount() {
        if (CollectionUtils.isNullOrEmpty(imageUrlList)) return 0;
        if (imageUrlList.size() == 4) return 5;
        return imageUrlList.size();
    }

    public void notifyDataSetChanged(List<String> imageUrlList) {
        this.imageUrlList = imageUrlList;
        this.notifyDataSetChanged();
    }

    final class ViewHolder extends RecyclerView.ViewHolder {

        public ViewHolder(final Context context, View view) {
            super(view);

            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    int position = getLayoutPosition();
                    if ((imageUrlList.size() == 4 && position < 2) || imageUrlList.size() != 4) {
                        FullScreenGridPreview imagePreview = new FullScreenGridPreview(context, imageViewMap, position);
                        imagePreview.startTransform(FullScreenGridPreview.STATE_TRANSFORM_IN);
                    } else if (imageUrlList.size() == 4 && position > 2) {
                        FullScreenGridPreview imagePreview = new FullScreenGridPreview(context, imageViewMap, position - 1);
                        imagePreview.startTransform(FullScreenGridPreview.STATE_TRANSFORM_IN);
                    }

                }
            });
        }
    }
}
